package org.jetlinks.pro.koudai.web;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.jetlinks.pro.koudai.Services.CourseService;
import org.jetlinks.pro.koudai.Services.TermsCourseClassService;
import org.jetlinks.pro.koudai.Services.TermsCourseService;
import org.jetlinks.pro.koudai.entity.CourseEntity;
import org.jetlinks.pro.koudai.entity.CourseInfo;
import org.jetlinks.pro.koudai.entity.TermsCourseClassEntity;
import org.jetlinks.pro.koudai.entity.TermsCourseEntity;
import org.jetlinks.pro.koudai.tenant.KoudaiAssetType;
import org.jetlinks.pro.tenant.AssetType;
import org.jetlinks.pro.tenant.crud.TenantCorrelatesAccessCrudController;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Nonnull;
import java.util.function.Function;

@RestController
@RequestMapping("/terms-course")
@Resource(id = "terms-course-manager", name = "学期课程管理")
@Tag(name = "学期课程管理")
@AllArgsConstructor
public class TermsCourseController implements TenantCorrelatesAccessCrudController<TermsCourseEntity, String> {

    private final TermsCourseService termsCourseService;
    private final TermsCourseClassService termsCourseClassService;



    @GetMapping("/{id:.+}/add-class")
    @QueryAction
    @Operation(summary = "添加班级")
    public Mono<SaveResult> addClass(@PathVariable @Parameter(description = "课程ID") String id, @RequestParam @Parameter(description = "班级ID") String classId) {
        TermsCourseClassEntity termsCourseClassEntity = new TermsCourseClassEntity(id, classId);
        return this.termsCourseClassService.save(termsCourseClassEntity);
    }
    //获取详情
    @GetMapping("/{id:.+}/remove-class")
    @QueryAction
    @Operation(summary = "移除班级")
    public Mono<Integer> removeClass(@PathVariable @Parameter(description = "课程ID") String id, @RequestParam @Parameter(description = "班级ID") String classId) {

        return this.termsCourseClassService.createDelete()
            .where(TermsCourseClassEntity::getClassId,classId)
            .and(TermsCourseClassEntity::getTermCourseId,id)
            .execute();
    }

    @Override
    public TermsCourseService getService() {
        return termsCourseService;
    }

    @Nonnull
    @Override
    public AssetType getAssetType() {
        return KoudaiAssetType.termsCourse;
    }

    @Nonnull
    @Override
    public Function<TermsCourseEntity, ?> getAssetIdMapper() {
        return TermsCourseEntity::getId;
    }

    @Nonnull
    @Override
    public String getAssetProperty() {
        return "termsCourse";
    }
}
